// pages/Login/Entercode/Entercode.js
const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    true: true,
    code: '',
    showInfo: true,
    InputValue:''
  },
  //获取邀请码
  getInputValue(e) {
    this.setData({
      code: e.detail.value
    })
    console.log(e.detail.value.length)
    if (e.detail.value.length == 13) {
      const data = {
        number: e.detail.value
      }
      this.getRefereeInfo(data)
    } else {
      this.setData({
        showInfo: true
      })
    }


  },
  //授权登录
  wxLogin(data) {
    var _this = this
    http.postRequest('v1/wxRegister', data,
      function (res) {
        getApp().globalData.token = res.data.token;
        wx.setStorageSync("token", res.data.token)
        _this.syncData({is_sync:1})
      },
      function (res) {
        _this.toastDialog.showDialog(res.msg)
      })
  },
  //获取缓存
  //缓存数据
  syncData(data) {
    var _this = this
    http.postRequest('syncData', data,
      function (res) {

        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      },
      function (res) {
        _this.toasts.hideToast({})
        _this.toastDialog.showDialog(res.msg)
      })
  },
  //获取推荐人信息
  getRefereeInfo(data) {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    var _this = this
    http.postRequest('v1/getRefereeInfo', data,
      function (res) {
        wx.hideToast()
        var nickName = res.data.nickname.split(''),
          nick = ''
        for (let i in nickName) {
          if (nickName[i] != ' ') {
            console.log(nickName[i])
            nick = nick + nickName[i]
          }
        }
        res.data.nickname = nick
        console.log(res.data, nick)
        _this.setData({
          nickname: nick,
          avatar_url: res.data.avatar_url,
          referee_data: JSON.stringify(res.data),
          showInfo: false
        })
      },
      function (res) {
        _this.toastDialog.showDialog(res.msg)
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const client_data = wx.getStorageSync('Info')
    const codes = options.code
    this.setData({
      client_data
    })
    this.setData({
      codes
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  Go: util.throttle(function (e) {
    if (this.data.showInfo) {
      this.toastDialog.showDialog('请填写正确的邀请码!')
    } else {
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
      wx.login({
        success: res => {
          var string = JSON.parse(this.data.referee_data)
          string.nickname = 'AA'
          const data = {
            client_data: JSON.stringify(this.data.client_data),
            code: res.code,
            referee_data: JSON.stringify(string)
          }
          this.wxLogin(data)
        }
      })
    }
  }, 1000),
  //扫码
  ScanCode() {
    wx.scanCode({
      success: res => {
        console.log(res)
        var result = res.result.split("=")
        console.log(result[1])
        this.setData({
          InputValue:result[1]
        })
        this.getRefereeInfo({
          number: result[1]
        })
      }
    })
  },
})